from flask import Blueprint, request

from app import get_db_connect
from app.common.auth import userSchema
from app.common.jwts import generate_jwt
from app.common.responses import re_response


login_bp = Blueprint("login_bp", __name__)

@login_bp.route('/adminLogin', methods=['POST'])
def login():
    data = request.get_json()
    username = data['username']
    password = data['password']
    connection = get_db_connect()
    with connection.cursor() as cursor:
        f = cursor.execute('SELECT * FROM users WHERE username = %s and password = %s', (username,password))
        user_data = cursor.fetchall()[0]
        schema = userSchema()
        user_data = schema.dump(user_data)
        useruuid = user_data['uuid']
        connection.commit()
    if(f):
        token = generate_jwt(useruuid,username,"display")
        print("登陆成功")
        return re_response(msg = "登陆成功",token = token, data = user_data)
    else:
        return re_response(code = 2001,msg = "用户名或密码错误")



